package com.ruoyi.wq.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.Condition;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wq.mapper.WqActivityMapper;
import com.ruoyi.wq.domain.WqActivity;
import com.ruoyi.wq.domain.vo.WqActivityVO;
import com.ruoyi.wq.service.IWqActivityService;

/**
 * 活动详情Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-12
 */
@Service
public class WqActivityServiceImpl extends ServiceImpl<WqActivityMapper, WqActivity> implements IWqActivityService {

    @Autowired
    private WqActivityMapper wqActivityMapper;
    
    @Autowired
    private ISysUserService sysUserService;
    
    /**
     * 查询活动详情
     * 
     * @param id 活动详情主键
     * @return 活动详情
     */
    @Override
    public WqActivity selectWqActivityById(String id)
    {
        return wqActivityMapper.selectWqActivityById(id);
    }

    /**
     * 查询活动详情列表
     * 
     * @param wqActivity 活动详情
     * @return 活动详情
     */
    @Override
    public List<WqActivity> selectWqActivityList(WqActivity wqActivity)
    {
        return wqActivityMapper.selectWqActivityList(wqActivity);
    }

    /**
     * 新增活动详情
     * 
     * @param wqActivity 活动详情
     * @return 结果
     */
    @Override
    public int insertWqActivity(WqActivity wqActivity)
    {
        wqActivity.setCreateTime(DateUtils.getNowDate());
        return wqActivityMapper.insertWqActivity(wqActivity);
    }

    /**
     * 修改活动详情
     * 
     * @param wqActivity 活动详情
     * @return 结果
     */
    @Override
    public int updateWqActivity(WqActivity wqActivity)
    {
        wqActivity.setUpdateTime(DateUtils.getNowDate());
        return wqActivityMapper.updateWqActivity(wqActivity);
    }

    /**
     * 批量删除活动详情
     * 
     * @param ids 需要删除的活动详情主键
     * @return 结果
     */
    @Override
    public int deleteWqActivityByIds(String[] ids)
    {
        return wqActivityMapper.deleteWqActivityByIds(ids);
    }

    /**
     * 删除活动详情信息
     * 
     * @param id 活动详情主键
     * @return 结果
     */
    @Override
    public int deleteWqActivityById(String id)
    {
        return wqActivityMapper.deleteWqActivityById(id);
    }

	@Override
	public TableDataInfo getPageList(WqActivity wqActivity) {
		IPage<WqActivityVO> page = baseMapper.selectPageList(Condition.getPage(wqActivity), wqActivity);
		for(WqActivityVO vo : page.getRecords()){
			if(vo.getStartTime() != null && vo.getEndTime() != null){
				String applyTime = DateUtils.getRangeTime(vo.getStartTime(), vo.getEndTime());
				vo.setApplyTime(applyTime);
			}
		}
		return Condition.getTableDataInfo(page.getRecords(), page.getTotal());
	}

	@Override
	public Long getSumNumByActivityThemeId(String activityThemeId) {
		QueryWrapper<WqActivity> queryWrapper = new QueryWrapper<>();
		WqActivity entity = new WqActivity();
		entity.setActivityThemeId(activityThemeId);
		queryWrapper.setEntity(entity);
		Integer count = baseMapper.selectCount(queryWrapper);
		return Long.valueOf(count);
	}

	@Override
	public TableDataInfo getPageActivityList(WqActivity wqActivity) {
		SysUser sysUser = sysUserService.getById(SecurityUtils.getUserId());
		if("0".equals(sysUser.getIsManager())){
			throw new ServiceException("权限不足");
		}
		IPage<WqActivityVO> page = baseMapper.selectPageList(Condition.getPage(wqActivity), wqActivity);
		for(WqActivityVO vo : page.getRecords()){
			if(vo.getStartTime() != null && vo.getEndTime() != null){
				String applyTime = DateUtils.getRangeTime(vo.getStartTime(), vo.getEndTime());
				vo.setApplyTime(applyTime);
			}
		}
		return Condition.getTableDataInfo(page.getRecords(), page.getTotal());
	}
	
}
